const fs = require("fs").promises;
const path = require("path");
const MAIN_PATH = path.resolve(__dirname, "..")
const XJB_PATH = path.resolve(__dirname, "..", "..", "新将包");
const exportFile = [
	"css/editor.css",
	"css/main.css",
	"js/import/importEditor.mjs",
	"js/editor.js",
	"md/skillEditor.md"
].map(url => {
	return path.resolve(MAIN_PATH, url);
});
const exportDir = [
	"js/editor",
	"js/tool",
	"js/interact"
].map(url => {
	return path.resolve(MAIN_PATH, url)
})
const sendFile = async (...files) => {
	const reads = files.map(file => {
		return fs.readFile(file);
	})
	const datas = await Promise.all(reads);
	const writes = datas.map((data, index) => {
		return fs.writeFile(files[index].replace(MAIN_PATH, XJB_PATH), data);
	})
	await Promise.all(writes)
}
const sendDir = async (...dirs) => {
	const status = dirs.map(dir => {
		return fs.readdir(dir)
	})
	const resources = (await Promise.all(status)).map((list, index) => {
		return list.map(name => path.resolve(dirs[index], name))
	}).flat();
	const stats = resources.map(resource => {
		return fs.stat(resource)
	})
	const isFiles = (await Promise.all(stats)).map(stat => {
		return stat.isFile;
	})
	const files = resources.filter((_, index) => {
		return isFiles[index];
	})
	sendFile(...files);
	console.log(files.join("\n"))
}
sendDir(...exportDir);
sendFile(...exportFile)